package com.tongtech.proxy.core.crypto;

import java.io.File;
import java.math.BigInteger;
import java.util.Base64;
import java.util.HashSet;

public class PrivateKey {
	public String getAlgorithm() {
		return "ssh-rsa";
	}

	// 私钥
	private BigInteger d;

	// 公钥
	private BigInteger e;

	// 摸
	private BigInteger n;

	public PrivateKey(BigInteger d, BigInteger e, BigInteger n) {
		this.d = d;
		this.e = e;
		this.n = n;
	}

	/**
	 * 私钥
	 * 
	 * @return
	 */
	public String getD() {
		return d.toString(KeyGenerator.BIGINTENCODING);
	}

	/**
	 * 公钥
	 * 
	 * @return
	 */
	public String getE() {
		return e.toString(KeyGenerator.BIGINTENCODING);
	}

	/**
	 * 摸
	 * 
	 * @return
	 */
	public String getN() {
		return n.toString(KeyGenerator.BIGINTENCODING);
	}

	/**
	 * 用公钥加密
	 * 
	 * @param m
	 * @return
	 */
	public BigInteger encrypt(BigInteger m) {
		return m.modPow(e, n);
	}

	/**
	 * 用私钥解密
	 * 
	 * @param m
	 * @return
	 */
	public synchronized BigInteger decrypt(BigInteger m) {
		return m.modPow(d, n);
	}

	@Override
	public int hashCode() {
		return d.hashCode() ^ e.hashCode() ^ n.hashCode();
	}

	public boolean equals(Object o) {
		if (!(o instanceof PrivateKey))
			return false;
		PrivateKey k = (PrivateKey) o;
		if (!this.d.equals(k.d))
			return false;
		if (!this.e.equals(k.e))
			return false;
		if (!this.n.equals(k.n))
			return false;
		return true;
	}

	private static String decrypt(String ciphertext, String decryptionKey) {
		try {
			byte[] encryptedBytes = Base64.getDecoder().decode(ciphertext);
			byte[] keyBytes = decryptionKey.getBytes();

			// 简单的异或解密示例
			byte[] decryptedBytes = new byte[encryptedBytes.length];
			for (int i = 0; i < encryptedBytes.length; i++) {
				decryptedBytes[i] = (byte) (encryptedBytes[i] ^ keyBytes[i % keyBytes.length]);
			}

			return new String(decryptedBytes);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public static final long MAGIC_CODE = 0x526463686e706c72l;

	// RSA密钥转字符串的进制值（36进制）
	private static final int RADIX = 36;

	private static final int OFFSET_MAGIC = 0;
	private static final int OFFSET_VERSION = 8;
	private static final int OFFSET_TOTAL = 16;
	private static final int OFFSET_EXPIRE = 24;
	private static final int OFFSET_TYPE = 32;
	private static final int OFFSET_HASHCHECK = 40;
	private static final int OFFSET_NUMBER_OF_GROUPS = 72;
	private static final int OFFSET_GROUP_TOTAL = 80;// 每套可用的最大内存
	private static final int OFFSET_PRODUCT_NAME = 88;
	private static final int OFFSET_CREATE_TIME = 96;
	private static final int OFFSET_PADDING = 104;
	private static final int OFFSET_MACS_NUMBER = 248;
	private static final int OFFSET_MACS = 256;

	private static final int MAX_LINE_LENGTH_IN_LICENSE_FILE = 64;

	// license允许使用的内存总量，单位byte
	private static long TotalMemary = 0;

	// 当前license过期时间
	private static long ExpiredTime = 0;

	// 当前license类型(标准版为64，企业版为101，云化版为111)
	private static long LicenseType = 0;

	private static long LoadTimestamp = 0;

	private static String EndUser = "";

	private static String ProjectName = "";

	private static boolean MacAuthorized = true;

	private static final HashSet<Long> AuthorizedMacs = new HashSet<>();

	private static final byte[] LICENSEFILE = new byte[]{(byte) 0x2f, (byte) 0x63, (byte) 0x45, (byte) 0x4e
			, (byte) 0x74, (byte) 0x45, (byte) 0x72, (byte) 0x2e, (byte) 0x6c, (byte) 0x69, (byte) 0x43};

	// 模
	private static final int[] model = {0xc061689c, 0xb7eaa0ea, 0x7b7394b2, 0x0ecd7e13, 0xcefbecf3, 0x4255d5f6, 0x2815a787, 0xfd50978d
			, 0x0aa63de8, 0x436b86a6, 0xca484476, 0x2a651196, 0xac80d4a7, 0xbe676e73, 0x04bff384, 0xc8dc3fd4
			, 0x88699d38, 0x1207f539, 0x47fea2b1, 0x1c729ff8, 0x15e5cfdc, 0x55d9cbfa, 0xc60eed15, 0x26586bff
			, 0x8f046d2e, 0xdb827eb8, 0x724557c2, 0x2d849432, 0x59aa8bff, 0x745dd97a, 0x6494562a, 0xf223b152
			, 0xf7865274, 0xdd9d16b9, 0x1394bfe2, 0x19220f25, 0xd4500fed, 0x8003b395, 0xc02f6917, 0x2e0ea312
			, 0xc1e91446, 0xf73d4b0f, 0xfc0d7005, 0xd6b96fe3, 0x41773e94, 0xd4c6fe97, 0x376a4e3e, 0x496687db
			, 0xf5379ae4, 0xd40e7b0f, 0x9725d2a3, 0x5c6d6942, 0xa92d103b, 0x20eb9741, 0x0ee03a63, 0xd57b5459
			, 0x830da717, 0xeb850345, 0xd415521c, 0xf8123ed6, 0xa1c5fea5, 0x911d1976, 0x37c85f78, 0xef1e5e11
			, 0x01f54eba, 0x02dd5fbf, 0x558c7764, 0x8c097528, 0xa0dbce36, 0xac205c22, 0xeb63702b, 0x370b8032
			, 0xcfca28c3, 0xdc1a590c, 0x85d1a5f4, 0xc0b7ee9c, 0xd30e382a, 0x162d4778, 0xfc7ad6f8, 0xe59b377b
			, 0x735be141, 0x8134d1c6, 0x2a4997df, 0x5280bad4, 0x40b84b41, 0xa3f1d9db, 0xcfc85558, 0x308f962a
			, 0x6e2bf59c, 0x70b74c6b, 0x5a7ebb95, 0x1e67f78b, 0xa0ea94a6, 0x9fd1bbcf, 0x158817b6, 0xfe7c5fdd
			, 0x557e29a7, 0x83e1dce7, 0xe7aaa671, 0x72bdd843, 0x6d085d93, 0x8352930d, 0x7664892f, 0xb4846080
			, 0x8da5c966, 0x7b53ed69, 0x11c5dce2, 0x71aac33f, 0x70f6fef4, 0x53b5ea46, 0x4a6842af, 0x234428cc
			, 0xd496c00d, 0xe0b08800, 0x3a3d6f43, 0x30e20dca, 0xc620d9db, 0x181c776d, 0x23e2e738, 0x5cb78397
			, 0xcb414b1a, 0xdeec7833, 0xa6ff057e, 0xdd0219f5, 0x97244a6f, 0x200fdf0c, 0x0b8be25f, 0x7831965b};

}
